//定义全局变量
var logGrid;// 表格
var dhxWins,queryLogWin;
var oneLogInfo;//日志项信息
// 页面布局
var pageObj = {type : "0100",height : [50, , 55 ], paging : true, size : 15}
var layoutContainer = create_layout(pageObj);

// 初始化操作
$(function() {
    initGrid();
    doOnSearchFormInit();
    findPageGrid();
});
// 初始化表格
function initGrid() {
    // 获取表格对象
    logGrid = layoutContainer.grid[0];
    // 定义表格表头
    var columnsInfo = [ {header: '操作员id', id: 'id',width: 50,hidden:true},
        {header: '医院编码',id: 'hosCode',width: 80, hidden:true},
        {header: '操作员名称',id: 'operator', width: 80},
        {header: '请求参数',id: 'requestParams',width: '*'},
        {header: '操作内容',id: 'content', width: 150,hidden:true},
        {header: '类名',id: 'className',width: 130 },
        {header: '方法名',id: 'methodName',width: 130, hidden:true},
        {header: '请求地址',id: 'url',width: 100},
        {header: 'ip地址', id: 'clientIp', width: 130},
        {header: '创建时间',id: 'gmtCreate',width: 150},
        {header: '持续时间',id: 'elapsedTime',width: 70} ];
    // 创建表格
    dmjplugs.grid.createGrid({
        sm : 'ch',
        skin : 'dhx_skyblue',
        srcc : true,
        columns : columnsInfo
    }, logGrid);
    logGrid.setTotal(0);
    logGrid.enableEditEvents(false, false, false);
    logGrid.setColumnHidden(0,true);
    // 注册分页事件
    logGrid.attachEvent("loadPagingData", function(first, limit) {
        findPageGrid(first, limit);
    });
    logGrid.attachEvent("onRowDblClicked",function () {
        queryDetail();
    });
}
// 根据选择条件,获取表格数据
function findPageGrid(first, limit) {
    // 初始化表格为空表
    if (!limit) {
        logGrid.setTotal(0);
        logGrid.clearAll();
        first = 0;
        limit = logGrid.getPageRowLimit();
    }

    // 分页组件数据
    var postData = "pageNo=" + parseInt(first / limit + 1) + "&pageSize=" + limit + "&pageCount=" + logGrid.getPageTotal();
    if (typeof (search_Form) == "object") {
        var startTimeCalendar = search_Form.getInput('startTime');
        var endTimeCalendar = search_Form.getInput('endTime');
        postData = postData +
            "&begintimeStr="+startTimeCalendar.value+
            "&endtimeStr="+endTimeCalendar.value+
            "&className="+$.trim(search_Form.getInput("className").value)+
            "&url="+$.trim(search_Form.getInput("url").value)+
            "&operator="+$.trim(search_Form.getInput("operator").value);
    }
    $.ajax({
        url : basePath + 'plfLog/findPageGrid',
        type : 'post',
        dataType : 'json', // 返回的数据类型
        data : encodeURI(postData),
        success : function(data) {
            if(!data){
                $func.alert('未找到相关日志','red');
                return false;
            }
            if(!data.result){
                $func.alert(data.message?data.message:'未找到相关日志','red');
                return false;
            }
            if(data.rowCount>0){
                logGrid.parse(data.gridr, "json");
                logGrid.setTotal(data.rowCount);
                logGrid.uncheckAll();
            }
        }
    });
}

// 顶部查询框初始化
function doOnSearchFormInit() {
    if (typeof (search_Form) == "object") {
        moduleSearchInit();
        search_Form.attachEvent("onButtonClick", function(name, command) {
            if (command == 'search')
                doRefresh();
            else if (command == 'reset')
                doReset();
        });
        $('input.dhxlist_txt_textarea').keypress(function(event){
            if(event.keyCode == 13){ //绑定回车
                doRefresh();
            }
        });
        var startTimeCalendar = search_Form.getCalendar('startTime');
        startTimeCalendar.setDateFormat("%Y-%m-%d %H:%i:%s");
        startTimeCalendar.showTime();
        // $('input[name=startTime]').attr('readonly',true);
        var endTimeCalendar = search_Form.getCalendar('endTime');
        endTimeCalendar.setDateFormat("%Y-%m-%d %H:%i:%s");
        endTimeCalendar.showTime();
        // $('input[name=endTime]').attr('readonly',true);

        //限制日期输入
        $("input[name='startTime']").keyup(function(){
            var tmptxt=$(this).val();
            $(this).val(tmptxt.replace(/[^0-9- :]/g,''));
        }).change(function(){
            var tmptxt=$(this).val();
            tmptxt=tmptxt.replace(/[^0-9- :]/g,'');
            $(this).val(tmptxt);
            if(!checkDateStr('yyyy-MM-dd hh:mm:ss',tmptxt)){
                $func.alert("创建时间起始录入不规范！");
                dateFlag=false;
            }else{
                dateFlag=true;
            }
        }).css("ime-mode", "disabled").attr('placeholder','yyyy-MM-dd hh:mm:ss').attr("maxlength",19);
        $("input[name='endTime']").keyup(function(){
            var tmptxt=$(this).val();
            $(this).val(tmptxt.replace(/[^0-9- :]/g,''));
        }).change(function(){
            var tmptxt=$(this).val();
            tmptxt=tmptxt.replace(/[^0-9- :]/g,'');
            $(this).val(tmptxt);
            if(!checkDateStr('yyyy-MM-dd hh:mm:ss',tmptxt)){
                $func.alert("创建时间截止录入不规范！");
                dateFlag1=false;
            }else{
                dateFlag1=true;
            }
        }).css("ime-mode", "disabled").attr('placeholder','yyyy-MM-dd hh:mm:ss').attr("maxlength",19);
    }
    if (typeof (butt_Form) == 'object') {
        butt_Form.attachEvent("onButtonClick",function(name,command) {
            if (command == 'queryDetail') //编辑
                queryDetail();
        });
    }
}

function doReset() {
    var startTimeCalendar = search_Form.getInput('startTime');
    startTimeCalendar.value = "";
    var endTimeCalendar = search_Form.getInput('endTime');
    endTimeCalendar.value = "";
    search_Form.getInput("className").value = '';
    search_Form.getInput("url").value = '';
    search_Form.getInput("operator").value = '';
}
function doRefresh() {
    findPageGrid();
}
function queryDetail() {
    var logs = getCheckedRowsByGrid(logGrid, logGrid.getColIndexById('select'));
    if (!logs || logs.length > 1){
        $func.alert("请选择一行进行查看", "red");
        return;
    }
    oneLogInfo=logs[0];
    if (!dhxWins)dhxWins = new dhtmlXWindows();
    dhxWins.setImagePath(basePP + "js/dhtmlx/dhtmlxWindows/codebase/imgs/");
    queryLogWin = dhxWins.createWindow("queryLogWin", 2, 2, 400,280);
    queryLogWin.center();
    queryLogWin.setModal(true);
    queryLogWin.bringToTop();
    queryLogWin.setText("<span style=\"font-size:14px;\">" + "日志详情"+ "</span>");
    queryLogWin.denyResize();
    queryLogWin.button("minmax1").hide();
    queryLogWin.button("park").hide();
    queryLogWin.attachURL(basePath + "plfLog/openLogDetailPage");
}